package com.du.stutestsys.controller;

import com.alibaba.fastjson.JSONObject;
import com.du.stutestsys.controller.BaseControler;
import com.du.stutestsys.entity.*;
import com.du.stutestsys.service.Administrator.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController extends BaseControler {
    @Autowired
    SysUserService sysUserService;

    @ResponseBody
    @GetMapping("/admin/login/{SysUser}")
    public String Login(@PathVariable SysUser user, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        SysUser sysUser = sysUserService.Login(user);
        System.out.println(sysUser.toString());
        if (null==sysUser){
            logger.info("登录验证失败");
            jsonObject.put("success",false);

        }else {
            logger.info("登录验证成功"+sysUser.getId());
            //保存登录id方便后续获取登录角色信息
            session.setAttribute("userId",sysUser.getId());
            //获取角色类别 如管理员、学生、老师等
            session.setAttribute("roleType",sysUser.getType());
            jsonObject.put("success",true);

        }
        return jsonObject.toJSONString();
    }

    @ResponseBody
    @GetMapping("/admin/Main")
    public String adminMain(HttpSession httpSession){
        Object userId = checkUser(httpSession);
        if (userId == null) {
            return "redirect:/admin/login";
        }
        return "";
    }
}
